<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace app\admin\controller;

use  Mscore\Core\Controller;
use  app\admin\model\Index as IndexModel;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lib\Tree;

class IndexController extends Controller{
	public $model;
    public $login;
  
    public function __construct()  
    {    
        $this->model = new IndexModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
    } 		
    /**
     * 首页默认方法
     * @param array $index 
     * @param array $where 
    */
    public function index(){
        $user=$this->login['info'];
        $user['header_url']=file_exists(config('file.path').$user['header_url'])?$user['header_url']:'';
        $muedata=(new LoginModel)->getRules();
        $mue = Tree::mue($muedata);
        $topmue = Tree::mue($muedata,1);
        $systemver=config("app.systemver");
        $sys_info ['ver']  = $systemver['ver'];
        unset($systemver);
        $this->assign("mue",$mue);
        $this->assign("topmue",$topmue);
	    $this->assign("sys_info",$sys_info);
		$this->assign("user",$user);
        $this->display("index.html");
    }

    /**
     * 左侧菜单
     * @param array $mueleft 
     * @param array $where 
    */
    public function mue(){
        $user['grade']=$this->login['info']['grade'];
        $user['roletype']=$this->login['info']['roletype'];
        //获取菜单数据
        $muedata=(new LoginModel)->getRules();
        $data = array(
                "code" => 200,
                "data" => array_values($muedata),
                "msg" => 'true'
            );
        $this->echojson($data);
    }
    
    /**
     * 首页默认控制台
     * @param array $console 
     * @param array $where 
    */
    public function console(){
        $sys_info=$this->model->console();
        $sys_info['withdraw']=round($sys_info['withdraw'], 2);
        $sys_info['recharge']=round($sys_info['recharge'], 2);
        $user=$this->login['info'];
        $this->assign("user",$user);
        $this->assign("sys_info",$sys_info);
        $this->display("console.html");
    }

    /**
     * 获取缓存
     * @param array $cache 
     * @param array $where 
    */
    public function cache(){
        //缓存文件
       $Path=$this->model->cache();
	   $this->assign ( 'compilePath', $Path['tempPath'].'/compile/' );
       $this->assign ( 'compilesize', $Path['compilesize'] );
       $this->assign ( 'cachePath', $Path['tempPath'].'/cache/' );
       $this->assign ( 'cachesize', $Path['cachesize'] );
	   $this->display('cache.html');
    }

    /**
     * 删除缓存
     * @param array $cachedel 
     * @param array $where 
    */
	public function cachedel(){
       //缓存文件
       $delfile=$this->model->cachedel(post('file'));
       if($delfile){
	   $data = array(
                "code" => 200,
                "data" => $delfile,
                "msg" => '删除成功'
            );
        $this->echojson($data);
	   };
    }

    /**
     * 配置地址
     * @param array $proaddress 
     * @param array $where 
    */
    public function proaddress(){
     if(get('type')=='gaode'){
       $this->display('gaodemap_api.html'); 
     }elseif(get('type')=='baidu'){
       $this->display('baidumap_api.html');
     }else{
       $this->display('baidumap_api.html');
     }
    }

    /**
     * 在线用户
     * @param array $useronline 
     * @param array $where 
    */
    public function useronline(){
        $page = post('page',1);
        $limit = post('limit',5);
        $where = '';
        if($this->login['info']['ranges'] < 1){
            $where .= ' pid ='.$this->login['info']['aid'].' or aid='.$this->login['info']['aid'];   
        }
        $useronline =$this->model->useronline($where,$page,$limit);
        $data = array(
                "code" => 200,
                "data" => $useronline['datalist'],
                "count"=> $useronline['total'],
                "msg" => 'success'
            );
        $this->echojson($data);
    }

    /**
     * 登陆记录
     * @param array $recordlogin 
     * @param array $where 
    */
    public function recordlogin(){
        $page = post('page',1);
        $limit = post('limit',5);
        $where = '';
        if($this->login['info']['ranges'] < 1){
            $where .= ' pid ='.$this->login['info']['aid'].' or aid='.$this->login['info']['aid'];
        }
        $record =$this->model->recordlogin($where,$page,$limit);
        $data = array(
                "code" => 200,
                "data" => $record['datalist'],
                "count"=> $record['total'],
                "msg" => 'success'
            );
        $this->echojson($data);
    }

    /**
     * 用户登录次数图表
     * @param array $online
     * @param array $where 
    */
    public function online(){
        $useronline =$this->model->online();
        $data = array(
                "code" => 200,
                // 'title' => '用户登录次数',
                "datalist" => [$useronline['user']],
                "time"=> $useronline['time'],
                "msg" => 'success'
            );
        $this->echojson($data);
    }

    /**
     * 用户设备图表
     * @param array $ondevice
     * @param array $where 
    */
    public function ondevice(){
        $ondevice =$this->model->ondevice();
        $data = array(
                "code" => 200,
                "datalist" => $ondevice['data'],
                "device"=> $ondevice['device'],
                "msg" => 'success'
            );
        $this->echojson($data);
    }

    /**
     * 用户设备图表
     * @param array $ondevice
     * @param array $where 
    */
    public function newuser(){
        $useronline =$this->model->newuser();
        $data = array(
                "code" => 200,
                "datalist" => $useronline['user'],
                "time"=> $useronline['time'],
                "msg" => 'success'
            );
        $this->echojson($data);
    }

    /**
     * 强制下线
     * @param array $offline 
     * @param array $where 
    */
    public function offline(){
    //缓存文件
        $offline=$this->model->offline(post('id'));
        if($offline){
            $data = array(
                "code" => 200,
                "msg" => 'success'
            );
            $this->echojson($data);
        }else{
            $data = array(
                "code" => 106,
                "msg" => 'fail'
            );
            $this->echojson($data);
        }
    }

}